package com.yq.cp.utils;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yq.cp.entity.data.NengKongData;
import com.yq.cp.mapper.data.NengKongDataMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;

@Component
public class RabbitMqReceiveUtil {

    @Resource
    NengKongDataMapper nengKongDataMapper;
    @RabbitListener(queues = "queue_data",concurrency = "1")
    @Transactional
    public void add(Message message, Channel channel) throws IOException {
        NengKongData javaObject = JSON.parseObject(message.getBody()).toJavaObject(NengKongData.class);
//        nengKongDataMapper.insert(javaObject);

//        String s = new String(message.getBody());
//        List<NengKongData> list = JSONObject.parseArray(s, NengKongData.class);
//        for (int i = 0; i < list.size(); i++) {
//            NengKongData nengKongData = list.get(i);
//            try{
//                nengKongDataMapper.insert(nengKongData);
//            }catch (Exception e){
//
//            }
//        }
        //手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}